/*
 * Copyright (c) 2013. wyouflf (wyouflf@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wordplat.quickstart.xutils.common.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created with IntelliJ IDEA.
 * User: wyouflf
 * Date: 13-6-19
 * Time: PM 1:18
 *
 * @param <K1> k1
 * @param <K2> k2
 * @param <V>  v
 * @since 2021-05-08
 */
public class DoubleKeyValueMap<K1, K2, V> {
    private final ConcurrentHashMap<K1, ConcurrentHashMap<K2, V>> k1ConcurrentHashMapConcurrentHashMap;

    /**
     * DoubleKeyValueMap
     */
    public DoubleKeyValueMap() {
        this.k1ConcurrentHashMapConcurrentHashMap = new ConcurrentHashMap<K1, ConcurrentHashMap<K2, V>>();
    }

    /**
     * put
     *
     * @param key1
     * @param key2
     * @param value
     */
    public void put(K1 key1, K2 key2, V value) {
        if (key1 == null || key2 == null || value == null) {
            return;
        }
        if (k1ConcurrentHashMapConcurrentHashMap.containsKey(key1)) {
            ConcurrentHashMap<K2, V> concurrentHashMap = k1ConcurrentHashMapConcurrentHashMap.get(key1);
            if (concurrentHashMap != null) {
                concurrentHashMap.put(key2, value);
            } else {
                concurrentHashMap = new ConcurrentHashMap<K2, V>();
                concurrentHashMap.put(key2, value);
                k1ConcurrentHashMapConcurrentHashMap.put(key1, concurrentHashMap);
            }
        } else {
            ConcurrentHashMap<K2, V> hashMap = new ConcurrentHashMap<K2, V>();
            hashMap.put(key2, value);
            k1ConcurrentHashMapConcurrentHashMap.put(key1, hashMap);
        }
    }

    /**
     * getFirstKeys
     *
     * @return Set<K1>
     */
    public Set<K1> getFirstKeys() {
        return k1ConcurrentHashMapConcurrentHashMap.keySet();
    }

    /**
     * get
     *
     * @param key1
     * @return ConcurrentHashMap
     */
    public ConcurrentHashMap<K2, V> get(K1 key1) {
        return k1ConcurrentHashMapConcurrentHashMap.get(key1);
    }

    /**
     * get
     *
     * @param key1
     * @param key2
     * @return V
     */
    public V get(K1 key1, K2 key2) {
        ConcurrentHashMap<K2, V> map = k1ConcurrentHashMapConcurrentHashMap.get(key1);
        return map == null ? null : map.get(key2);
    }

    /**
     * getAllValues
     *
     * @param key1
     * @return Collection
     */
    public Collection<V> getAllValues(K1 key1) {
        ConcurrentHashMap<K2, V> hashMap = k1ConcurrentHashMapConcurrentHashMap.get(key1);
        return hashMap == null ? null : hashMap.values();
    }

    /**
     * getAllValues
     *
     * @return Collection
     */
    public Collection<V> getAllValues() {
        Collection<V> result = null;
        Set<K1> k1Set = k1ConcurrentHashMapConcurrentHashMap.keySet();
        if (k1Set != null) {
            result = new ArrayList<V>();
            for (K1 k1 : k1Set) {
                ConcurrentHashMap<K2, V> value1 = k1ConcurrentHashMapConcurrentHashMap.get(k1);
                if (value1 != null) {
                    Collection<V> values = value1.values();
                    if (values != null) {
                        result.addAll(values);
                    }
                }
            }
        }
        return result;
    }

    /**
     * containsKey
     *
     * @param key1
     * @param key2
     * @return boolean
     */
    public boolean containsKey(K1 key1, K2 key2) {
        if (k1ConcurrentHashMapConcurrentHashMap.containsKey(key1)) {
            ConcurrentHashMap<K2, V> value1 = k1ConcurrentHashMapConcurrentHashMap.get(key1);
            if (value1 != null) {
                return value1.containsKey(key2);
            }
        }
        return false;
    }

    /**
     * containsKey
     *
     * @param key1
     * @return boolean
     */
    public boolean containsKey(K1 key1) {
        return k1ConcurrentHashMapConcurrentHashMap.containsKey(key1);
    }

    /**
     * size
     *
     * @return int
     */
    public int size() {
        if (k1ConcurrentHashMapConcurrentHashMap.size() == 0) {
            return 0;
        }
        int result = 0;
        for (ConcurrentHashMap<K2, V> hashMap : k1ConcurrentHashMapConcurrentHashMap.values()) {
            result += hashMap.size();
        }
        return result;
    }

    /**
     * remove
     *
     * @param key1
     */
    public void remove(K1 key1) {
        k1ConcurrentHashMapConcurrentHashMap.remove(key1);
    }

    /**
     * remove
     *
     * @param key1
     * @param key2
     */
    public void remove(K1 key1, K2 key2) {
        ConcurrentHashMap<K2, V> hashMap = k1ConcurrentHashMapConcurrentHashMap.get(key1);
        if (hashMap != null) {
            hashMap.remove(key2);
        }
        if (hashMap == null || hashMap.isEmpty()) {
            k1ConcurrentHashMapConcurrentHashMap.remove(key1);
        }
    }

    /**
     * clear
     */
    public void clear() {
        if (k1ConcurrentHashMapConcurrentHashMap.size() > 0) {
            for (ConcurrentHashMap<K2, V> hashMap : k1ConcurrentHashMapConcurrentHashMap.values()) {
                hashMap.clear();
            }
            k1ConcurrentHashMapConcurrentHashMap.clear();
        }
    }
}
